package com.xwyzsn.backend.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.xwyzsn.backend.job.common.Result;
import com.xwyzsn.backend.job.entity.Student;
import com.xwyzsn.backend.job.entity.Teacher;
import com.xwyzsn.backend.job.mapper.StudentMapper;
import com.xwyzsn.backend.job.mapper.TeacherMapper;
import com.xwyzsn.backend.job.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-03
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {


    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    public Result login(String uname, String pwd) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("snum", uname);
        Student stu = studentMapper.selectOne(queryWrapper);
        if (stu == null) {
            QueryWrapper<Teacher> queryWrappertea = new QueryWrapper<>();
            queryWrappertea.eq("tnum", uname);
            Teacher tea = teacherMapper.selectOne(queryWrappertea);
            if (tea == null) {
                return Result.fail("user not exist");
            }
            if (!tea.getPassword().equals(pwd)) {
                return Result.fail("password error");
            }
            return Result.success(tea);


        }
        if (!stu.getPassword().equals(pwd)) {
            return Result.fail("password error");
        }
        return Result.success(stu);
    }

    public Result getInfo(String uname) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("snum", uname);
        Student stu = studentMapper.selectOne(queryWrapper);
        if (stu == null) {
            return Result.fail("user not exist");
        }
        return Result.success(stu);
    }

    public Result update(String snum, String confirmPassword, String email, String tel, String qq) {
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("snum", snum);
        updateWrapper.set("email", email);
        updateWrapper.set("tel", tel);
        updateWrapper.set("qq", qq);
        updateWrapper.set("password", confirmPassword);
        studentMapper.update(null, updateWrapper);
        return new Result(200, "success", null);
    }

    public Result getDeptStudent(String dept) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept", dept);
        return Result.success(studentMapper.selectList(queryWrapper));
    }

    public Result updateAll(String snum, String sname, Integer age, String tel, String qq, String confirmPassword, String email, String classnum, String dept, String major) {
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("snum", snum);
        updateWrapper.set("sname", sname);
        updateWrapper.set("age", age);
        updateWrapper.set("tel", tel);
        updateWrapper.set("qq", qq);
        updateWrapper.set("password", confirmPassword);
        updateWrapper.set("email", email);
        updateWrapper.set("classnum", classnum);
        updateWrapper.set("dept", dept);
        updateWrapper.set("major", major);
        int res = studentMapper.update(null, updateWrapper);
        if (res == 0) {
            return Result.fail("update fail");
        }
        return new Result(200, "success", null);


    }

    public Result deleteBatch(List<Integer> sids) {

        int res = studentMapper.deleteBatchIds(sids);
        if (res == 0) {
            return Result.fail("delete fail");
        }
        return new Result(200, "success", null);


    }

    public Result uploadCsv(MultipartFile uploadfile) throws IOException {
        final byte[] bytes = uploadfile.getBytes();
        // check tmp folde
        final Path path = Paths.get(uploadfile.getOriginalFilename());
        Files.write(path, bytes);
        // cmd调用 python脚本
        String[] args = new String[]{"python", "E:\\桌面\\work\\backend\\src\\main\\java\\db_script.py","E:\\桌面\\work\\backend\\"+ path.toString()};
        Process proc = Runtime.getRuntime().exec(args);// 执行py文件
        // 获取exit code
        int exitVal = 0;
        try {
            exitVal = proc.waitFor();
            if(exitVal == 0) {
                return new Result(200, "success", null);
            }else{
                return new Result(500, "fail", null);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return new Result(500, "fail", null);
        }
    }
}
